<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>站停频率与使用率分析 - 哈尔滨公交数据分析</title>
    <link rel="stylesheet" href="css/style.css?v=1.1">
</head>

<body>
    <div class="navbar">
        <a href="index.html">首页</a>
        <a href="traffic_trend.html">客流趋势分析</a>
        <a href="station_ranking.html">站点客流排行</a>
        <a href="funnel_analysis.html">线路站点客流分析</a>
        <a href="line_comparison.html">线路客流量对比</a>
        <a href="transfer_heatmap.html">换乘热度分析</a>
        <a href="poi_analysis.html">站点周边客流特征</a>
        <a href="anomaly_detection.html">异常客流检测</a>
        <a href="od_analysis.html">路径OD分析</a>
        <a href="stop_frequency_usage_analysis.html">站停频率分析</a>
        <a href="dispatch_suggestion.html">智能调度模型</a>
    </div>
    <div class="page-container">
        <div class="chart-header">
            <h2 class="chart-title">公交线路站停频率与使用率关联分析</h2>
            <p class="chart-description">
                此散点图展示了公交站点"停靠频率"与"使用率"之间的关系。每个点代表一个站点。通过观察站点的分布，可以识别出高频率高使用率的核心站点（右上角）、高频率低使用率的资源闲置站点（右下角）或低频率高使用率的潜力站点（左上角），为线路优化提供数据支持。
            </p>
        </div>
        <div class="kpi-cards-container">
            <div class="kpi-card">
                <h4>核心站点代表</h4>
                <p id="kpi-core-station">--</p>
            </div>
            <div class="kpi-card">
                <h4>潜力站点代表</h4>
                <p id="kpi-potential-station">--</p>
            </div>
            <div class="kpi-card">
                <h4>闲置站点代表</h4>
                <p id="kpi-idle-station">--</p>
            </div>
            <div class="kpi-card">
                <h4>分析站点总数</h4>
                <p id="kpi-total-stations">--</p>
            </div>
        </div>
        <div id="main" style="width: 100%; height:600px;"></div>
    </div>

    <script src="js/echarts.min.js"></script>
    <script type="text/javascript">
        var mainChart = echarts.init(document.getElementById('main'));

        fetch('/api/bus/stop-frequency-usage')
            .then(response => response.json())
            .then(data => {
                const chartData = data.map(item => ({
                    name: item.stationName,
                    value: [item.totalStops, item.onboard + item.offboard]
                }));

                // --- KPI Card Calculations ---
                if (chartData.length > 0) {
                    const totalStops = chartData.reduce((sum, item) => sum + item.value[0], 0);
                    const totalUsage = chartData.reduce((sum, item) => sum + item.value[1], 0);
                    const avgStops = totalStops / chartData.length;
                    const avgUsage = totalUsage / chartData.length;

                    const coreStations = chartData.filter(d => d.value[0] > avgStops && d.value[1] > avgUsage);
                    const potentialStations = chartData.filter(d => d.value[0] < avgStops && d.value[1] > avgUsage);
                    const idleStations = chartData.filter(d => d.value[0] > avgStops && d.value[1] < avgUsage);

                    const findTopStation = (stations, valueIndex, isMax) => {
                        if (stations.length === 0) return { name: "无" };
                        return stations.reduce((best, current) => {
                            if (isMax) {
                                return current.value[valueIndex] > best.value[valueIndex] ? current : best;
                            }
                            return current.value[valueIndex] < best.value[valueIndex] ? current : best;
                        });
                    };

                    document.getElementById('kpi-core-station').textContent = findTopStation(coreStations, 1, true).name;
                    document.getElementById('kpi-potential-station').textContent = findTopStation(potentialStations, 1, true).name;
                    document.getElementById('kpi-idle-station').textContent = findTopStation(idleStations, 0, true).name;
                    document.getElementById('kpi-total-stations').textContent = `${chartData.length} 个`;
                }
                // --- End of KPI Card Calculations ---

                var option = {
                    title: {
                        text: '站停频率与使用率关联分析',
                        subtext: '数据来源: 后端数据库',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: function (params) {
                            return params.data.name + '<br/>' +
                                '停靠频率: ' + params.data.value[0] + ' 次<br/>' +
                                '使用人次: ' + params.data.value[1];
                        }
                    },
                    xAxis: {
                        type: 'value',
                        name: '站停频率 (总停靠次数)',
                        splitLine: {
                            show: true
                        }
                    },
                    yAxis: {
                        type: 'value',
                        name: '使用率 (总上下车人数)',
                        splitLine: {
                            show: true
                        }
                    },
                    series: [{
                        name: '站点',
                        type: 'scatter',
                        symbolSize: 10,
                        data: chartData,
                        emphasis: {
                            focus: 'series',
                            label: {
                                show: true,
                                formatter: function (params) {
                                    return params.data.name;
                                },
                                position: 'top'
                            }
                        }
                    }]
                };
                mainChart.setOption(option);
            })
            .catch(error => {
                console.error('获取站停频率数据失败:', error);
                const chartDom = document.getElementById('main');
                chartDom.innerHTML = '<p style="text-align:center;color:red;padding-top:50px;">数据加载失败，请确保后端服务已启动且数据已导入。</p>';
            });

        window.addEventListener('resize', function () {
            mainChart.resize();
        });
    </script>
    <script src="js/common.js"></script>
</body>

</html>